<script>
import {createProps} from '@/utils';
import FieldRegistry from '@/components/hhd/design/FormField/FieldRegistry';
import Constants from '@/components/hhd/design/FormEngine/Constants';

export default {
  name: 'FieldDelegate',
  props: {
    fieldConfig: createProps(Object),
    context: createProps(Object),
    formModel: createProps(Object),
    rowIndex: createProps(Number),
  },
  render() {
    if(!this.fieldConfig || !this.context) return undefined
    let comp = FieldRegistry[this.fieldConfig.component];
    return comp && this.renderField(comp) || undefined;
  },
  methods: {
    renderField(comp) {
      if(this.formModel[Constants.authFieldName][this.fieldConfig.fieldName] === 'hide') return
      return this.formModel && this.$createElement(comp, {
        props: {
          value: this.formModel[this.fieldConfig.fieldName],
          auth: this.formModel[Constants.authFieldName][this.fieldConfig.fieldName],
          fieldConfig: this.fieldConfig,
          rowIndex: this.rowIndex,
        },
        on: {
          'update:value': v => this.formModel[this.fieldConfig.fieldName] = v,
          'update:title': t => this.fieldConfig.fieldTitleName && (this.formModel[this.fieldConfig.fieldTitleName] = t),
        }
      }) || undefined;
    },
  }
};
</script>